package stream;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.comparator.PropertyComparator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class CollectorsTest {
    public static void main(String[] args) {
        List<Student> studentList = CollUtil.toList(new Student().setId(1).setAge(10), new Student().setId(2).setAge(9), new Student().setId(3).setAge(11));

        List<Student> collect = studentList.stream().collect(Collectors.toCollection(CollUtil::newArrayList));
        Student age = studentList.stream().collect(Collectors.minBy(new PropertyComparator<>("age"))).get();
        Student age1 = studentList.stream().collect(Collectors.maxBy(new PropertyComparator<>("age"))).get();

        Integer collect1 = studentList.stream().collect(Collectors.summingInt(Student::getAge));
        Double collect2 = studentList.stream().collect(Collectors.averagingInt(Student::getAge));

        Student student3 = studentList.stream().collect(
                Collectors.reducing((student1, student2) -> CompareUtil.compare(student1.getAge(), student2.getAge()) > 0 ? student1 : student2)
        ).get();
        Integer integer = studentList.stream().map(student -> student.getAge()).collect(
                Collectors.reducing((studentAge1, studentAge2) -> studentAge1 + studentAge2)
        ).get();

        Map<Integer, Integer> collect3 = studentList.stream().collect(Collectors.toMap(Student::getId, Student::getAge, (oldValue, newValue) -> newValue));

        Map<Integer, List<Student>> collect4 = studentList.stream().collect(Collectors.groupingBy(Student::getId));
        Map<Integer, Set<Student>> collect5 = studentList.stream().collect(Collectors.groupingBy(Student::getId, Collectors.toSet()));

        Map<Integer, List<Integer>> collect6 = studentList.stream().collect(
                Collectors.groupingBy(Student::getId, Collectors.mapping(student -> student.getAge(), Collectors.toList()))
        );

        HashMap<Integer, Set<Integer>> collect7 = studentList.stream().collect(
                Collectors.groupingBy(Student::getId, HashMap::new, Collectors.mapping(student -> student.getAge(), Collectors.toSet()))
        );
    }
}
